package com.picture.download.controller;

import com.picture.download.Settings;
import com.picture.download.entity.Category;
import com.picture.download.entity.Images;
import com.picture.download.util.ResponseUtil;
import com.picture.download.util.Utils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.lang.Files;
import org.nutz.lang.Strings;
import org.nutz.lang.random.R;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by chenjidong on 2015/4/23.
 */
@At("/category")
public class CategoryController {
    private static Dao dao = Settings.getDao();

    @At("/")
    @Ok("json")
    @AdaptBy(type = JsonAdaptor.class)
    public Object index(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("::filter") Map<String, String> filter, @Param("::sort") List<String> sort) {
        if (pageNum == null || pageNum == 0) pageNum = 1;
        if (pageSize == null || pageSize == 0) pageSize = 20;

        Pager pager = dao.createPager(pageNum, pageSize);
        Criteria cri = Utils.setupFilter(Cnd.cri(), filter, sort);

        List<Category> list = dao.query(Category.class, cri, pager);
        pager.setRecordCount(dao.count(Category.class, cri));

        return ResponseUtil.response(0, "", list);
    }

    @At
    @Ok("json")
    public Object get(String oid) {
        Category category;

        category = dao.fetch(Category.class, Cnd.where("oid", "=", oid));


        return ResponseUtil.response(0, "", category);
    }

    @At
    @Ok("json")
    public Object getByName(String name) {
        Category category;
        category = dao.fetch(Category.class, Cnd.where("name", "=", name));
        if (null == category) {
            return ResponseUtil.response(1, "", "");
        }
        return ResponseUtil.response(0, "", category);
    }


    /**
     * 设置条目
     *
     * @param max
     * @param filePath
     * @return
     */
    @At
    @Ok("json")
    public Object set(Integer max) {
        if (null == max || max == 0) {
            max = 15;
        }

        for (int i = 0; i < max; i++) {
            switch (i) {
                case 0:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\mote", "美女车模");
                    break;
                case 1:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\mingxingxiezhen", "明星写真");
                    break;
                case 2:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\donghuameinv", "卡通动漫");
                    break;
                case 3:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\fengjing", "建筑风景");
                    break;
                case 4:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\langman", "浪漫爱情");
                    break;
                case 5:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\tiyu", "体育竞技");
                    break;
                case 6:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\youxi", "炫酷游戏");
                    break;
                case 8:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\yingshi", "影视精选");
                    break;
                case 9:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\mingche", "世界名车");
                    break;
                case 10:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\keai", "清新可爱");
                    break;
                case 11:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\dongwu", "动物萌宠");
                    break;
                case 12:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\huacao", "花草植物");
                    break;
                case 13:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\shierxingzuo", "十二星座");
                    break;
                case 14:
                    setCategory("C:\\Users\\chenjidong\\Pictures\\meishi", "创意美食");
                    break;
                default:

                    setCategory("", "未知分类");
                    break;
            }
        }

        return ResponseUtil.response(0, "已更新 " + max + " 个条目", true);
    }

    public void setCategory(String path, String categoryName) {
        if (null != path && !Strings.equals("", path)) {

            Category category = new Category();
            category.setName(categoryName);
            category.setOid(R.UU64());
            category.setCreatedOn(new Date());
            category.setSort("" + 100);
            List<Images> images = new ArrayList<>();
            File file = new File(path);
            int i = 0;
            if (file.listFiles() != null && file.listFiles().length > 0) {
                for (File f : file.listFiles()) {
                    String imgName = R.UU64();
                    String prefix = f.getName().substring(f.getName().lastIndexOf(".") + 1);
                    File newFile = new File(Settings.uploadImgPath + "/" + imgName + "." + prefix);
                    boolean isSuccess = Files.copy(f, newFile);
                    if (isSuccess) {
                        Images images1 = new Images();
                        images1.setOid(imgName);
                        images1.setName("图片" + i);
                        images1.setCreatedOn(new Date());
                        images1.setSort("1000" + i);
                        images1.setSuffix(prefix);
                        images1.setCategoryName(category.getName());
                        images.add(images1);
                    }
                }
                i += 1;
            }
            category.setImages(images);
            category.setImagesCount(images.size());
            dao.insertWith(category, "images");

        }
    }
}
